package com.ice.app.system.user.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.entity.User;
import com.ice.app.system.mapper.MenuMapper;
import com.ice.app.system.organ.service.OrganService;
import com.ice.app.system.organ.vo.OrganTreeVo;
import com.ice.app.system.role.service.RoleService;
import com.ice.app.system.role.vo.RoleTreeVo;
import com.ice.app.system.user.service.UserService;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Page;
import com.iceframework.core.entity.Result;
import com.iceframework.core.support.security.entity.LogInfo;
import com.iceframework.core.support.security.entity.PermissionInfo;
import com.iceframework.core.utils.WebUtils;

import io.swagger.annotations.Api;

/**
 * 
 * 控制层： 系统用户
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/sys/user")
@Api(value = "用户Controller", tags = { "用户操作接口" })
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private OrganService organService;

	@Autowired
	private MenuMapper menuMapper;

	/**
	 * 更新的数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getUser(@RequestParam(value = "key") String key, HttpServletRequest request) {
		User user = userService.getUser(key);
		return Result.success().add("user", user);
	}

	/**
	 * 用户信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	public Result getUser(HttpServletRequest request) {
		User user = userService.getUserByUsername(WebUtils.getUserName());
		return Result.success().add("user", user);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result userIndexData(User user, Page<User> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderByScript("username=username,mobile=mobile"));
		List<User> users = userService.getUsers(user);
		PageInfo<User> data = new PageInfo<User>(users, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param user
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveUser(User user) throws Exception {
		if (userService.saveUser(user)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delUser(String keys) {
		if (userService.deleteUsers(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 密码重置
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/passwdReset", method = RequestMethod.POST)
	public Result resetRwd(String key) throws Exception {
		return userService.saveResetPwd(key);
	}

	/**
	 * 账号禁用
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/disable", method = RequestMethod.POST)
	public Result disableUser(String keys) throws Exception {
		if (userService.disableUser(keys)) {
			return Result.success().setMsg("禁用用户成功");
		} else {
			return Result.fail().setMsg("禁用用户失败");
		}
	}

	/**
	 * 账号启用
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/enable", method = RequestMethod.POST)
	public Result enableUser(String keys) throws Exception {
		if (userService.enableUser(keys)) {
			return Result.success().setMsg("启用用户成功");
		} else {
			return Result.fail().setMsg("启用用户失败");
		}
	}

	/**
	 * 加载配置角色树页面
	 * 
	 * @param key
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/role/tree/data", method = RequestMethod.GET)
	public Result roleTreeData(@RequestParam(value = "key") String key, HttpServletRequest request) {
		List<RoleTreeVo> roleTree = roleService.getMenuTreeByRoleId(key);
		return Result.success().add("data", roleTree);
	}

	/**
	 * 保存角色和用户的关联
	 * 
	 * @param roles
	 * @param userId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/role/tree/save", method = RequestMethod.POST)
	public Result saveRoleUserRel(@RequestParam(value = "ids") String roles, @RequestParam(value = "key") String userId, HttpServletRequest request) throws Exception {
		if (roleService.saveRoleUserRel(userId, roles)) {
			return Result.success();
		} else {
			return Result.fail();
		}
	}

	/**
	 * 加载配置组织机构树页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/organ/tree/data", method = RequestMethod.GET)
	public Result organTreeIndexData(@RequestParam(value = "key") String key, HttpServletRequest request) {
		List<OrganTreeVo> organTree = organService.getOrganTreeByUserId(key);
		return Result.success().add("data", organTree);
	}

	/**
	 * 加载配置组织机构树页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/info/organ/tree/data", method = RequestMethod.GET)
	public Result organInfoTreeIndexData(@RequestParam(value = "key") String key, HttpServletRequest request) {
		List<OrganTreeVo> organTree = organService.getOrganTreeInfoByUserId(key);
		return Result.success().add("data", organTree);
	}

	/**
	 * 保存角色和菜单的联系
	 * 
	 * @param role
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/organ/tree/save", method = RequestMethod.POST)
	public Result saveOrganUserRel(@RequestParam(value = "ids") String organIds, @RequestParam(value = "key") String userId, HttpServletRequest request) throws Exception {
		if (organService.saveOrganUserRel(userId, organIds)) {
			return Result.success();
		} else {
			return Result.fail();
		}
	}

	@RequestMapping(value = "/user/un/{username}/permissions", method = RequestMethod.GET)
	public List<PermissionInfo> getPermissionByUsername(@PathVariable("username") String username) {
		System.out.println("getPermissionByUsername:" + username);
		List<PermissionInfo> permissionInfos = new ArrayList<>();
		PermissionInfo p = new PermissionInfo();
		p.setCode("111111");
		p.setMenu(username);
		permissionInfos.add(p);
		return permissionInfos;
	}

	@RequestMapping(value = "/permissions", method = RequestMethod.GET)
	@ResponseBody
	List<PermissionInfo> getAllPermissionInfo() {
		System.out.println("getAllPermissionInfo:");
		List<PermissionInfo> permissionInfos = new ArrayList<>();
		PermissionInfo p = new PermissionInfo();
		p.setCode("111111");
		permissionInfos.add(p);
		return permissionInfos;
	}

	@RequestMapping(value = "/log/save", method = RequestMethod.POST)
	public void saveLog(LogInfo info) {
		System.out.println(info);
	}

	@RequestMapping(value = "/user/resource/{username}/permissions", method = RequestMethod.GET)
	public List<String> getPermissionResourceByUsername(@PathVariable("username") String username) {
		return menuMapper.selectResourceByUserName(username);
	}

}